package com.xwdeshj.dal;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;


/**
 * Created by BKWJA on 2019/1/28.
 * 农家图集—SQL类
 * 因jdbcTemplate 加载有相应的顺序，故在该类注解就会报错。
 */
public class Dal_XwfarmAtlasinfo {


    /**
     * 判断该农家图集是否存在
     */
    public JSONArray getfarmAtlasinfo(String business_encryptId,String farm_encryptId,String id,JdbcTemplate jdbcTemplate){

        JSONArray arr = new JSONArray();
        String sql = "SELECT\n" +
                "  farm_atlas_id AS id,\n" +
                "  business_encryptId ,\n" +
                "  farm_encryptId,\n" +
                "  farm_atlas_img AS imgurl\n" +
                "FROM xw_farm_atlasinfo\n";

        StringBuffer sb = new StringBuffer();
        sb.append(" 1=1 ");
        //商家加密id
        if(!business_encryptId.equals("")){
            sb.append(" and business_encryptId in('"+business_encryptId+"') ");
        }
        //店铺加密id
        if(!farm_encryptId.equals("")){
            sb.append(" and farm_encryptId in('"+farm_encryptId+"') ");
        }
        //图集id
        if(!id.equals("")){
            sb.append(" and farm_atlas_id in('"+id+"') ");
        }
        try {
            sql = sql + " WHERE "+sb.toString();

            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
            while (rs.next()) {
                JSONObject object = new JSONObject();
                object.put("id",rs.getString("id") == null ? "" : rs.getString("id"));
                object.put("business_encryptId",rs.getString("business_encryptId") == null ? "" : rs.getString("business_encryptId"));
                object.put("farm_encryptId",rs.getString("farm_encryptId") == null ? "" : rs.getString("farm_encryptId"));
                object.put("imgurl",rs.getString("imgurl") == null ? "" : rs.getString("imgurl"));
                arr.add(object);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return arr;
    }

    /**
     * 弹框中获取对应店铺下的 图片图集
     */
    public JSONObject getfarmaltlasinfowin(String farm_encryptId,JdbcTemplate jdbcTemplate){
        JSONObject result = new JSONObject();
        String sql = "SELECT\n" +
                "  farm_atlas_id AS id,\n" +
                "  business_encryptId ,\n" +
                "  farm_encryptId,\n" +
                "  farm_atlas_img AS imgurl\n" +
                "FROM xw_farm_atlasinfo\n";

        StringBuffer sb = new StringBuffer();
        sb.append(" 1=1 ");
        //图集id
        if(!farm_encryptId.equals("")){
            sb.append(" and farm_encryptId in('"+farm_encryptId+"')");
        }
        try {
            sql = sql + " WHERE "+sb.toString();

            SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
            result.put("status",0);
            result.put("msg","ok");

            JSONArray jsonArray = new JSONArray();
            while (rs.next()) {
                JSONObject object = new JSONObject();
                object.put("id", (rs.getString("id") == null ? "" : rs.getString("id")));
                object.put("business_encryptId", (rs.getString("business_encryptId") == null ? "" : rs.getString("business_encryptId")));
                object.put("farm_encryptId", (rs.getString("farm_encryptId") == null ? "" : rs.getString("farm_encryptId")));
                object.put("imgurl", (rs.getString("imgurl") == null ? "" : rs.getString("imgurl")));
                jsonArray.add(object);
            }
            result.put("list", jsonArray);

        }catch (Exception e){
            e.printStackTrace();
            result.put("status", 40002);
            result.put("msg", e);
        }

        return result;
    }

}
